package com.example.textswitchtestdemo01.activity;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.example.textswitchtestdemo01.R;
import com.example.textswitchtestdemo01.bean.NewsInfo;
import com.example.textswitchtestdemo01.util.RetrofitUtil;

import java.text.SimpleDateFormat;
import java.util.List;

public class NewsAdater extends ArrayAdapter<NewsInfo.DataBean> {
    public NewsAdater(@NonNull Context context, @NonNull List<NewsInfo.DataBean> objects) {
        super(context, R.layout.item_lv_eighth_vp, objects);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_lv_eighth_vp, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        NewsInfo.DataBean item = getItem(position);
        holder.tv_title_lv_eight.setText(item.getInformationName());
        holder.tv_content_lv_eight.setText(item.getWords());
        long time = item.getTime();
        long l = System.currentTimeMillis() / 1000L;
        long min = (l - time) / 60L;
        Log.d("nn", "getView: " + new SimpleDateFormat("yyyy-MM-dd").format(time * 1000L));
        holder.tv_time_lv_eight.setText(min + "分钟前");
        String icon = item.getIcon();
        if (icon != null && !icon.isEmpty() && !icon.equals("null")) {
            Glide.with(getContext())
                    .load("http://" + RetrofitUtil.IP + item.getIcon())
                  //  .placeholder(R.drawable.ic_launcher_background)
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .override(120, 80)
                    .into(holder.img_photo_lv_eight);
        } else {
            holder.img_photo_lv_eight.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHolder {
        public View rootView;
        public TextView tv_time_lv_eight;
        public ImageView img_photo_lv_eight;
        public TextView tv_title_lv_eight;
        public TextView tv_content_lv_eight;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_time_lv_eight = (TextView) rootView.findViewById(R.id.tv_time_lv_eight);
            this.img_photo_lv_eight = (ImageView) rootView.findViewById(R.id.img_photo_lv_eight);
            this.tv_title_lv_eight = (TextView) rootView.findViewById(R.id.tv_title_lv_eight);
            this.tv_content_lv_eight = (TextView) rootView.findViewById(R.id.tv_content_lv_eight);
        }

    }
}
